

foreach xxx in  30_60 60_180 180_300 300_600 600_900 ///
				30_15 60_30 180_60 300_180 600_300 {
	clear
	use "cleandata\winprob_matched_dataset`xxx'"
	
	gen prev_check=strpos(prev_move,"+")>0
	gen prev_capture=strpos(prev_move,"x")>0
	
	keep if winprob!=. & timetaken!=. & timecontrol_iv!=. & size_match==2
	
	bys matchvar: egen start_clock1=total(start_clock*(timecontrol_iv==0))
	
	xtile start_clockbin=-start_clock1, n(5)

	
	
	bys matchvar: egen timetaken_1=total(timetaken*timecontrol_iv)
	bys matchvar: egen timetaken_0=total(timetaken*!timecontrol_iv)
	gen firststage=timetaken_1-timetaken_0 if size_match==2
		
	gen marginalvalue_firststage=marginalvalueoftime*firststage
	
	
	tempfile results
	forvalues t=1/5{		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==start_clockbin, absorb(matchvar#prev_check#prev_capture) cluster(matchvar)
		
		summ marginalvalue_firststage if start_clockbin==`t'
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & start_clockbin==`t'
		local meanfirststage=r(mean)
		
		summ start_clock1 if start_clockbin==`t'
		local meanstart_clock=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			gen estimate_type="Marginal Benefit"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			gen controls="Check and Capture"
			cap append using `results'
			save `results', replace
		restore
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==start_clockbin, absorb(matchvar#prev_check#prev_capture) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			gen estimate_type="Marginal Cost"
			gen controls="Check and Capture"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==start_clockbin, absorb(matchvar#piecesonboard) cluster(matchvar)
		
		summ marginalvalue_firststage if start_clockbin==`t'
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & start_clockbin==`t'
		local meanfirststage=r(mean)
		
		summ start_clock1 if start_clockbin==`t'
		local meanstart_clock=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			gen estimate_type="Marginal Benefit"
			gen controls="Number of Pieces"
			cap append using `results'
			save `results', replace
		restore
		
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==start_clockbin, absorb(matchvar#prev_check#prev_capture) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			gen estimate_type="Marginal Cost"
			gen controls="Number of Pieces"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
		
		ivreghdfe winprob (timetaken=timecontrol_iv) if `t'==start_clockbin, absorb(matchvar) cluster(matchvar)
		
		summ marginalvalue_firststage if start_clockbin==`t'
		local meanmarginalvalue=r(mean)
		summ firststage if marginalvalueoftime!=. & start_clockbin==`t'
		local meanfirststage=r(mean)
		
		summ start_clock1 if start_clockbin==`t'
		local meanstart_clock=r(mean)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			gen estimate_type="Marginal Benefit"
			gen controls="None"
			cap append using `results'
			save `results', replace
		restore
		
		
		
		replace marginalvalue_firststage=0 if timecontrol_iv==0
	
		ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv) if `t'==start_clockbin, absorb(matchvar) cluster(matchvar)
		
		preserve
			parmest, fast level(95)
			gen time="`xxx'"
			gen start_clockbin=`t'
			gen meanstart_clock=`meanstart_clock'
			gen estimate_type="Marginal Cost"
			gen controls="None"
			//gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
			cap append using `results'
			save `results', replace
		restore
		
	}
	preserve
	use `results', clear
	summ 
	
	
	
	if "`xxx'"=="30_60" | "`xxx'"=="30_15"{
		local mytitle="30 seconds"
	}
	if "`xxx'"=="60_180" | "`xxx'"=="60_30"{
		local mytitle="1 minute"
	}
	if "`xxx'"=="180_300" | "`xxx'"=="180_60"{
		local mytitle="3 minutes"
	}
	if "`xxx'"=="300_180" | "`xxx'"=="300_600"{
		local mytitle="5 minutes"
	}
	if "`xxx'"=="600_300" | "`xxx'"=="600_900"{
		local mytitle="10 minutes"
	}
	
	summ meanstart_clock
	
	replace meanstart_clock=meanstart_clock-(r(max)-r(min))/30 if controls=="None"
	replace meanstart_clock=meanstart_clock+(r(max)-r(min))/30 if controls=="Number of Pieces"
	
	
	
	//disp "Hello"
	twoway rspike min95 max95 meanstart_clock if controls=="None" & estimate_type=="Marginal Benefit", color(forest_green) || ///
		scatter estimate meanstart_clock if controls=="None" & estimate_type=="Marginal Benefit", color(forest_green ) c(l) msym(O) || ///
		rspike min95 max95 meanstart_clock if controls=="Number of Pieces" & estimate_type=="Marginal Benefit", color(maroon) || ///
		scatter estimate meanstart_clock if controls=="Number of Pieces" & estimate_type=="Marginal Benefit", color(maroon) c(l)  msym(S ) || ///
		rspike min95 max95 meanstart_clock if controls=="Check and Capture" & estimate_type=="Marginal Benefit", color(navy) || ///
		scatter estimate meanstart_clock if controls=="Check and Capture" & estimate_type=="Marginal Benefit", color(navy) c(l) msym(T) || ///
		rspike min95 max95 meanstart_clock if controls=="None" & estimate_type=="Marginal Cost", color(dkorange) || ///
		scatter estimate meanstart_clock if controls=="None" & estimate_type=="Marginal Cost", color(dkorange) c(l) lpattern(dash) msym(T) ///
		name(start_clocks_`xxx', replace) nodraw title("`mytitle'") legend(order(2  6 4 8) label(2 "Marginal Benefit") label(6 "Check/Capture Controls") label (4 "Number of Pieces Control") label(8 "Marginal Cost")) ///
		xtitle("Clock (seconds)") yscale(range(0)) ylabel(#3) xlabel(#3) yline(0, lcolor(gs12))
	
	restore
	//disp "Hello"
}
//
//
// graph combine start_clocks_30_15 start_clocks_60_30 start_clocks_180_60 start_clocks_300_180 start_clocks_600_300 start_clocks_900_600, name(lower, replace)
// graph export "start_clock_comparison_lower.pdf", as(pdf) replace
//
// graph combine start_clocks_15_30 start_clocks_30_60 start_clocks_60_180 start_clocks_180_300 start_clocks_300_600 start_clocks_600_900, name(higher, replace)
// graph export "start_clock_comparison_higher.pdf", as(pdf) replace



grc1leg  start_clocks_30_60 start_clocks_60_180 start_clocks_180_300 start_clocks_300_600 start_clocks_600_900 start_clocks_30_15 start_clocks_60_30 start_clocks_180_60 start_clocks_300_180 start_clocks_600_300, row(2) 
graph export "exhibits/start_clock_comparison_robust.pdf", as(pdf) replace
